$(function(){
	//自动加载
	getAdminList();

});

//加载列表
function getAdminList() {

//获取用户信息列表
	$("#adminlist").html("");
	$.ajax({
		url: urlcore + "/api/admin/selectList",
		type: "GET",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				var str="";
				$.each(data.data,function(i,n){
					var id = ""+n.id+"";
					if(n.available==1){
						str="可用";
					}else if(n.available==2){
						str="不可用";
					}
					var adminlist = 
					'	<tr>'+
					'		<td>'+n.loginName+'</td>'+
					'		<td>'+n.roleName+'</td>'+
					'		<td>'+str+'</td>'+
					'		<th>'+new Date(n.gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss")+'</th>'+
					'		<td>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" data-toggle="modal" data-target="#add" onclick="updateAdmin('+id+','+n.loginName+')"> 修改角色</a>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" onclick="updatePassword('+id+')"> 重置密码</a>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" onclick="delateAdmin('+id+')"> 删除</a>'+
					'		</td>'+
					'	</tr>';
					$('#adminlist').append(adminlist);
				});
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		}
	});

}

//搜索
function toSearch() {
	var userName = $('#userName').val().trim();
	var nick = $('#nick').val().trim();
	getAdminList(userName,nick);	
}

//删除
function delateAdmin(id) {
	if(confirm("确定要删除吗？")) {
		$.ajax({
			url: urlcore + "/api/admin/deleteAdmin?id="+id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
				   window.location.reload();
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				} 
			}
		});
	}
}

//添加/修改
function toAdd() {
	var adminId = sessionStorage.getItem("adminId");
	var loginName = $('#login_name').val().trim();
	var name = $('#name').val().trim();
	var password = $('#password').val().trim();
	var aginPassword = $('#password2').val().trim();
	var roleId = $('#select').val();
	if("请先添加角色"==$('#menu').text()){
		alert($('#menu').text());
		return;
	}
	if (password != aginPassword) {
		alert("两次密码不一致");
		return;
	}
	if(loginName==""){
       alert("账号不能为空！");
       return;
	}
	
	if(adminId != '' && adminId!=null) {
		$.ajax({
			url: urlcore + "/api/admin/updateAdmin?roleId="+roleId,
			type: "POST",
			dataType: 'json',
			data:JSON.stringify({
				  "id":adminId,
				  "loginName":loginName,
				  "name":name,
				  "password":password
			}),
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
					location.reload();
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				} else {
					if (data.msg != '空数据') {
						alert(data.msg)
					}
				}

			}
		});
		
	}else{
			$.ajax({
			url: urlcore + "/api/admin/add?roleId="+roleId,
			type: "POST",
			dataType: 'json',
			data:JSON.stringify({
				  "loginName":loginName,
				  "name":name,
				  "password":password
			}),
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
					location.reload();
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				}
			}
		});
	}
	
}

//加载修改数据
function updateAdmin(id,loginName) {
	sessionStorage.setItem("adminId",id);
    $('#this_title').html('修改管理员');
	$('#login_name').val(loginName);
    $('#menu').html("");
	$.ajax({
			url: urlcore + "/api/adminRole/findRoleList",
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				var da = data.data;
				var checkbox = "";
				if(da.length==0){
					checkbox+="<label class=\"col-sm-3 control-label\">请先添加角色</label>"
				}else{
					checkbox+="<select style=\"height:31px;border-radius: 3px;border: solid 1px #ddd;\" id=\"select\">";  
				$.each(da,function(i,n){
					checkbox +='<option value="'+n.id+'">'+n.name+'</option>';
				});
				checkbox+="</select>";
				}
				$('#menu').append(checkbox);
			}
		});

}

//获取所有权限
function thistitle() {
	$('#menu').html("");
	$.ajax({
			url: urlcore + "/api/adminRole/findRoleList",
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				var da = data.data;
				var checkbox = "";
				if(da.length==0){
					checkbox+="<label class=\"col-sm-3 control-label\">请先添加角色</label>"
				}else{
					checkbox+="<select style=\"height:31px;border-radius: 3px;border: solid 1px #ddd;\" id=\"select\">";  
				$.each(da,function(i,n){
					checkbox +='<option value="'+n.id+'">'+n.name+'</option>';
				});
				checkbox+="</select>";
				}
				$('#menu').append(checkbox);
			}
		});

	
	$('#this_title').html('添加管理员');
	$('#userName_add').val('');
	$('#password_add').val('');
	$('#aginPassword_add').val('');
	$('#realName_add').val('');
	$('#adminId').val('');
}

//重置密码
function updatePassword(id) {
	if(confirm("确定要修改密码？")) {
		$.ajax({
			url: urlcore + "/api/admin/updatePassword?adminId="+id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
					window.location.reload();
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				}

			}
		});
	}
}

